#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <errno.h>
#include "i18n.h"
#include "text.h"
#include "video.h"

#ifndef ENABLE_PANGO
#include <SDL_ttf.h>

static TTF_font* font;

static std::vector<std::string> TextWrapString(char* str, int width, bool split)
{
	int val;
	char tmp;
	char* start;
	char* end;
	char* word;
	std::vector<std::string> lines;
	
	if (*str == '\0')
		return lines;
	word = NULL;
	start = str;
	end = start + 1;
	while (*end != '\0')
	{
		tmp = *end;
		*end = '\0';
		
		if (tmp == '\n')
		{
			lines.push_back(std::string(start));
			start = end;
			word = NULL;
		}
		else if (split && tmp == ' ' && end[1] == ' ')
		{
			lines.push_back(std::string(start));
			start = end;
			word = NULL;
			*end = tmp;
			end += 2;
			continue;
		}
		else
		{
			if (tmp == ' ')
				word = end;
			TTF_SizeUTF8(font, start, &val, NULL);
			if (val > width)
			{
				if (word != NULL)
				{
					*end = tmp;
					end = word;
					tmp = *end;
					*end = '\0';
				}
				lines.push_back(std::string(start));
				start = end;
				word = NULL;
			}
		}
		
		*end = tmp;
		end++;
	}
	
	lines.push_back(std::string(start));
	
	return lines;
}

static void TextPrintUTF8(int x, int y, const char* str)
{
	SDL_Color fg = {255, 255, 255, 255};
	SDL_Surface* surface = TTF_RenderUTF8_Blended(font, str, fg);
	SDL_Rect dst = (x, y, 1, 1};
	SDL_BlitSurface(surface, NULL, screen, &dst);
	SDL_FreeSurface(surface);
}

static void TextPrintRAW(int x, int y, const char* str)
{
	char tmp[5000];
	
	ConvertToUTF8(str, tmp, 5000);
	SDL_Color fg = {255, 255, 255, 255};
	SDL_Surface* surface = TTF_RenderUTF8_Blended(font, tmp, fg);
	SDL_Rect dst = {x, y, 1, 1};
	SDL_BlitSurface(surface, NULL, screen, &dst);
	SDL_FreeSurface(surface);
}
#else

#include <SDL_Pango.h>



#endif
